package utils;

import io.PermutationReader;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

public class Driver {
	private PermutationReader reader;
	private PrintWriter wr;
	private Database db;
	public Driver(Database database,InputStream stream,String outputFile) throws IOException{
		wr=new PrintWriter(new BufferedWriter(new FileWriter(outputFile)));
		reader=new PermutationReader(stream);
		db=database;
		analyze();
		reader.close();
		
	}
	private void analyze() throws IOException{
		String permutation="";
		int mone;
                Map organisms=db.getOrganisms();
		int size=organisms.size();
		while ((permutation=reader.getNext())!=null){
			mone=0;
			Iterator iter=organisms.keySet().iterator();
                        while (iter.hasNext()){
                            String key=(String) iter.next();
                            ArrayList sequences = (ArrayList) organisms.get(key);
                            boolean flag=false;
                            for (int i=0;i<sequences.size() ;i++){
                                if (((String)sequences.get(i)).indexOf(permutation)!=-1){
                                    flag=true;                                    
                                }
                            }
                            if (flag) 
                                mone++;
                        }
			wr.println(permutation +" , "+ mone);
                        
		}
                wr.close();
		
	}

}
